﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace nyxwin.Common.Silverlight.Controls
{
    public partial class AppBarLikeButton : UserControl
    {
        private EventHandler executeChangedHandler;

        public AppBarLikeButton()
        {
            InitializeComponent();

            LayoutRoot.DataContext = this;
            this.executeChangedHandler = new EventHandler(newVal_CanExecuteChanged);

            CheckCanExecute();
        }

        public Uri IconUri
        {
            get { return (Uri)GetValue(IconUriProperty); }
            set { SetValue(IconUriProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IconUri.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IconUriProperty =
            DependencyProperty.Register("IconUri", typeof(Uri), typeof(AppBarLikeButton), new PropertyMetadata(null, new PropertyChangedCallback(
                (obj, evArgs) =>
                {
                    var thisObj = obj as AppBarLikeButton;
                    var newUri = evArgs.NewValue as Uri;

                    if (thisObj != null)
                    {
                        thisObj.button.Content = newUri;
                    }
                })));

        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Command.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.Register("Command", typeof(ICommand), typeof(AppBarLikeButton), new PropertyMetadata(null, new PropertyChangedCallback(
                (obj, evArgs) =>
                {
                    var newVal = evArgs.NewValue as ICommand;
                    var oldVal = evArgs.OldValue as ICommand;
                    var thisObj = obj as AppBarLikeButton;

                    if (thisObj == null)
                        return;

                    if (oldVal != null)
                    {
                        oldVal.CanExecuteChanged -= thisObj.executeChangedHandler;
                    }

                    if (newVal != null)
                    {
                        newVal.CanExecuteChanged += thisObj.executeChangedHandler;
                    }

                    thisObj.CheckCanExecute();
                })));

        private void newVal_CanExecuteChanged(object sender, EventArgs e)
        {
            CheckCanExecute();
        }

        private void CheckCanExecute()
        {
            var command = this.Command;

            if (command != null)
            {
                if (command.CanExecute(CommandParameter))
                {
                    button.IsEnabled = true;
                    return;
                }
            }

            button.IsEnabled = false;
        }

        private void Execute()
        {
            var command = this.Command;

            if (command != null)
            {
                command.Execute(CommandParameter);
            }
        }

        public object CommandParameter
        {
            get { return (object)GetValue(CommandParameterProperty); }
            set { SetValue(CommandParameterProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CommandParameter.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.Register("CommandParameter", typeof(object), typeof(AppBarLikeButton), new PropertyMetadata(null, new PropertyChangedCallback(
                (obj, evArgs) =>
                {
                    var thisObj = obj as AppBarLikeButton;

                    if (thisObj != null)
                        thisObj.CheckCanExecute();
                })));

        private void button_Click(object sender, RoutedEventArgs e)
        {
            Execute();
        }
    }
}
